from memory.unsafe import Pointer


struct HeapArray:
    var data: Pointer[Int]
    var size: Int

    # 自定义构造函数
    fn __init__(inout self, size: Int, val: Int):
        self.size = size
        self.data = Pointer[Int].alloc(self.size)
        for i in range(self.size):
            self.data.store(i, val)

    # 自定义复制
    fn __copyinit__(inout self, other: Self):
        self.size = other.size
        self.data = Pointer[Int].alloc(self.size)
        for i in range(self.size):
            self.data.store(i, other.data.load(i))

    # 自定义析构函数
    fn __del__(owned self):
        self.data.free()

    # 当源生命周期结束时将值从一个地方转移到另一个地方
    # 自定义移动函数据 __moveinit__
    # 自定义造函__takeinit__

    fn dump(self):
        print_no_newline("[")
        for i in range(self.size):
            if i > 0:
                print_no_newline(", ")
            print_no_newline(self.data.load(i))
        print("]")



struct UniquePointer:
    var ptr: Int

    fn __init__(inout self, ptr: Int):
        self.ptr = ptr

    # 用于想要对值取得独占所有权的函
    fn __moveinit__(inout self, owned existing: Self):
        self.ptr = existing.ptr

    fn __del__(owned self):
        self.ptr = 0

struct MyPair:
    var first: Int
    var second: Int

    fn __init__(inout self, first: Int, second: Int):
        self.first = first
        self.second = second

    fn dump(self):
        print("first:", self.first, ", second:", self.second)
